package com.xdwanj.tools;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


public class DevelopmentMethod extends OriginalMethod {
    final String key = ".exe";

    //获取该文件夹里所有文件
    public List GetFiles(File directory) {
        List<File> files = new ArrayList<>();
        //调用原方法类的GetPath获取该文件夹所有的文件及文件夹给列表files
        files = super.GetPath(directory);
        //遍历列表files并把列表中所有文件夹去掉
        for (int i = 0; i < files.size(); i++) {
            if (files.get(i).isDirectory()) {
                files.remove(i);
                i--;
            }
        }
        //处理完后返回列表
        return files;
    }

    //获取该文件夹里的所有文件夹//和上面那个方法处理方式类似不过是反过来把文件去掉（ps：懒得再解析流程 LOL）
    public List GetDirectory(File directory) {
        List<File> files = new ArrayList<>();
        files = super.GetPath(directory);
        for (int i = 0; i < files.size(); i++) {
            if (files.get(i).isFile()) {
                files.remove(i);
                i--;
            }
        }
        return files;
    }

    //文件过滤器
    public List FileFilters(File directory, String FileSuffix) {
        List<File> files = new ArrayList<>();
        //创建过滤器对象
        FileFilerImp fileFilerImp = new FileFilerImp(FileSuffix);
        //获取文件里所有文件
        files = super.GetPath(directory);
        //把列表files里不符合条件的都过滤掉
        for (int i = 0; i < files.size(); i++) {
            if (!fileFilerImp.accept(files.get(i))) {
                files.remove(i);
                i--;
            }
        }
        return files;
    }

    //复制粘贴文件
    @Override
    public File copy(File FileName, File target) {
        //如果粘贴处target不是个文件就对其进行处理
        if (!target.isFile()) {
            StringBuffer str = new StringBuffer((String.valueOf(FileName)));
            //判断粘贴处是否为盘符
            if (String.valueOf(target).length() == 3) {
                str.delete(0, ((String.valueOf(FileName))).lastIndexOf('\\') + 1);
                str.insert(0, target);
                target = new File(String.valueOf(str));
            } else {
                str.delete(0, ((String.valueOf(FileName)).lastIndexOf("\\")));
                str.insert(0, target);
                target = new File(String.valueOf(str));
            }
        }
        //处理完调用原方法类的Copy方法
        super.copy(FileName, target);
        return target;
    }

    public File copy(File FileName, File target, String Suffix) {
        List<File> files = FileFilters(FileName, Suffix);
        List<File> files1 = GetDirectory(FileName);
        for (int i = 0; i < files.size(); i++) {
            files.set(i, GetChildPath(files.get(i), FileName));
            files.set(i, CreatNewFileName(files.get(i), target));
            files1.set(i, GetChildPath(files1.get(i), FileName));
            files1.set(i, CreatNewFileName(files1.get(i), target));
            CreatNewDirectory(files1.get(i));
            CreatNewFile(files.get(i));
        }
        return target;
    }

    //复制粘贴文件夹
    public File copyUp(File FileName, File target) {

        List<File> files2 = new ArrayList<>();
        List<File> files3 = new ArrayList<>();
        File Target;
        File file = null;
        File file1;
        File file2;
        int j = 0;
        //分别用列表files2，files3接收文件和文件夹
        files2 = GetFiles(FileName);
        files3 = GetDirectory(FileName);
        //判断源文件夹是否为子文件夹
        for (char i : String.valueOf(FileName).toCharArray()) {
            if (i == '\\') {
                j++;
            }
        }
        //如果是列表files2，files3里的文件和文件夹都会扔去原方法类的方法GetChildPath处理，处理后会files2，files3里的路径名替换掉（ps：看这段代码之前记得吃降压药（ps：如有不适症状代码作者概不负责））
        if (j >= 2) {
            for (int i = 0; i < files2.size(); i++) {
                file1 = files2.get(i);
                file1 = super.GetChildPath(file1, FileName);
                files2.set(i, file1);
            }
            for (int i = 0; i < files3.size(); i++) {
                file2 = files3.get(i);
                file2 = super.GetChildPath(file2, FileName);
                files3.set(i, file2);
            }
        }
        //92行到103行的单次循环步骤分别是创建新文件夹路径名并根据此路径名创建文件夹，创建新文件路径名并根据此路径名创建文件
        for (int i = 0; i < files3.size(); i++) {
            file2 = files3.get(i);
            Target = super.CreatNewFileName(file2, target);
            super.CreatNewDirectory(Target);
            if (i == 0) {
                file = Target;
            }
        }
        for (int i = 0; i < files2.size(); i++) {
            file1 = files2.get(i);
            Target = super.CreatNewFileName(file1, target);
            super.CreatNewFile(Target);
            super.copy((File) GetFiles(FileName).get(i), Target);
        }
        return file;
    }

    //把源文件自动复制粘贴到U盘//你（王杰）写的我不解释了LOL
    public File copyTOUSBDrive(File FileName) {
        File f = null;
        File file1 = null;
        int j = 0;
        for (int i = 0; i <= 23; i++) {
            StringBuffer stringBuffer = new StringBuffer(":\\");
            stringBuffer.insert(0, (char) ('Z' - i));

            File file = new File(String.valueOf(stringBuffer));
            if (file.exists()) {
                StringBuffer strb = new StringBuffer(":\\");
                strb.insert(0, (char) ('Z' - i));
                f = new File(String.valueOf(strb));

                break;
            }
        }
        if (FileName.isDirectory()) {
            this.copyUp(FileName, f);
        } else {
            this.copy(FileName, f);
        }

        if (FileName.isDirectory()) {
            //获取u盘创建出来的文件夹
            for (char i : String.valueOf(FileName).toCharArray()) {
                if (i == '\\') {
                    j++;
                }
            }
            if (j >= 2) {
                file1 = super.GetChildPath(FileName, FileName);
            }
            f = super.CreatNewFileName(file1, f);
            return f;
        } else {
            StringBuffer str = new StringBuffer((String.valueOf(FileName)));
            //判断粘贴处是否为盘符
            str.delete(0, ((String.valueOf(FileName))).lastIndexOf('\\') + 1);
            str.insert(0, f);
            f = new File(String.valueOf(str));
            return f;
        }
    }

    //删除文件夹
    public void deleteDirectory(File directory) {
        List<File> files1 = new ArrayList<>();
        List<File> files2 = new ArrayList<>();
        File file;
        //先拿到列表目录
        files1 = GetPath(directory);
        //然后把他们顺序倒过来
        for (int i = files1.size(); i > 0; i--) {
            files2.add(files1.get(i - 1));
        }
        ///根据新的顺序进行删除
        for (int i = 0; i < files2.size(); i++) {
            file = files2.get(i);
            file.delete();
        }
    }

    //出自（王杰）我之手，这个你就不会封装一下，(#‵′)凸
    public void deleteAll(File val) {
        if (val.isFile()) {
            val.delete();
        } else {
            deleteDirectory(val);
        }
    }

    //剪切文件
    public File cut(File FileName, File target) {
        target = copy(FileName, target);
        FileName.delete();
        return target;
    }

    //剪切文件夹
    public File cutDirectory(File directory, File target) {
        target = copyUp(directory, target);
        deleteDirectory(directory);
        return target;
    }

    //出自（王杰）我之手，这个你就不会封装一下，(#‵′)凸
    public void cutAll(File fileName, File target) {
        if (fileName.isFile()) {
            cut(fileName, target);
        } else if (fileName.isDirectory()) {
            cutDirectory(fileName, target);
        }
    }

    //出自（王杰）我之手，这个你就不会封装一下，(#‵′)凸
    public File copyAll(File fileName, File target) {
        if (fileName.isFile()) {
            return copy(fileName, target);
        } else {
            return copyUp(fileName, target);
        }
    }

    public File copyAll(File fileName, File target, String key) {
        return copy(fileName, target, key);
    }

    //创建快捷方式
    public List createShortcuts(File val, String key) {
        List<File> fileList = null;
        List pathList = new ArrayList();
        String filename = null;
        String name = null;
        String suffix = null;
        File folder = null;

        folder = new File("快捷方式");
        folder.mkdir();

        if (val.isFile()) {
            filename = val.getName();
            name = filename.substring(0, filename.indexOf("."));

            try {
                // ShellLink.createLink(val.toString(), "快捷方式\\" + name + ".lnk");
                pathList.add(new File(name + ".lnk"));
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            fileList = FileFilters(val, key);

            for (File fs : fileList) {
                int i = 1;

                //文件的完整名称,如spring.jpeg
                filename = fs.getName();
                //文件名,如spring
                name = filename.substring(0, filename.indexOf("."));
                //文件后缀,如.jpeg
                suffix = filename.substring(filename.lastIndexOf("."));
                //目标文件
                File descFile = new File("快捷方式\\" + name + ".lnk");

                //若文件存在重命名
                while (descFile.exists()) {
                    String newFilename = "快捷方式\\" + name + "(" + i + ")" + ".lnk";
//                    String parentPath = descFile.getParent();
                    descFile = new File(newFilename);
                    i++;
                    System.out.println(descFile.toString());
                }
                try {
                    // ShellLink.createLink(fs.toString(), descFile.toString());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                pathList.add(new File(descFile.toString()));
            }
        }
        return pathList;
    }

    //上边的方法的重载，用于默认检索词
    public List createShortcuts(File val) {
        return createShortcuts(val, this.key);
    }

    //忽视传入的是文件还是文件夹，将所有内容返回
    public List getPathAll(File val) {
        List pathList = new ArrayList();

        if (val.isFile()) {
            pathList.add(val);
            return pathList;
        } else {
            pathList = GetPath(val);
            return pathList;
        }
    }
}
